<!-- 我申请的年审 -->
<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form :model="searchInfo" :inline="true" size="small">
        <el-form-item label="项目名称">
          <el-input v-model="searchInfo.name" placeholder="请输入项目名称" clearable />
        </el-form-item>
        <el-form-item label="开通时间">
          <el-date-picker
            v-model="timeValue"
            style="width: 300px;"
            clearable
            size="small"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            value-format="yyyy-MM-dd HH:mm:ss"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="onSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="ticketList" stripe>
        <el-table-column label="#" type="index" width="80" align="center" />
        <el-table-column label="项目名称" prop="name" align="center" :show-overflow-tooltip="true" />
        <el-table-column label="项目分类" prop="projectClassName" align="center" />
        <el-table-column label="上报时间" prop="reportTime" align="center" />
        <el-table-column label="最近一次年审" prop="lastYearAuditTime" align="center" />
        <el-table-column label="流转状态" prop="statusName" align="center" />
        <el-table-column label="结果" prop="auditStatusName" align="center" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
          <template slot-scope="scope">
            <el-button v-if="scope.row.lastYearAuditTime && scope.row.auditStatus == 1" v-permisaction="['apply:MyAnnual']" size="mini" type="text" icon="el-icon-edit" @click="handleApply(scope.row)">申请年审</el-button>
            <el-button v-if="scope.row.lastYearAuditTime && scope.row.auditStatus !== 1" v-permisaction="['detail:MyAnnual']" size="mini" type="text" icon="el-icon-edit" @click="handleView(scope.row,false)">表单数据</el-button>
            <el-button v-if="scope.row.lastYearAuditTime && scope.row.auditStatus !== 1" v-permisaction="['history:MyAnnual']" size="mini" type="text" icon="el-icon-delete" @click="handleHistoryView(scope.row)">审批历史</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pageInfo.total > 0" :total="pageInfo.total" :page.sync="pageInfo.currentPage" :limit.sync="pageInfo.pageSize" @pagination="onPagination" />
    </el-card>
    <ApplyDialog ref="applyDialog" @refreshTable="getList" />
    <ReviewDialog ref="reviewDialog" @refreshTable="getList" />
    <HistoryDialog ref="historyDialog" source="ns" />
  </div>
</template>

<script>
import {
  doMyAuditApplication
} from '@/api/xjs/work-order'
import ReviewDialog from './components/review.vue'
import HistoryDialog from '../components/history'
import { mapGetters } from 'vuex'
// 搜索
import { parseTime } from '@/utils'
import { projectClassList, auditStatusList } from '@/utils/common'
import ApplyDialog from './components/applyDialog.vue'
export default {
  name: 'MyDraft',
  components: { ApplyDialog, ReviewDialog, HistoryDialog },
  computed: {
    ...mapGetters(['circulationStatus'])
  },
  data() {
    return {
      searchInfo: {
        name: '', // 项目名称
        startOpeningTime: '', // 最近保存开始时间
        endOpeningTime: '' // 最近保存结束时间
      },
      searchInfoCopy: null, // 用于重置
      pageInfo: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      projectClassList: projectClassList, // 项目分类列表
      auditStatusList: auditStatusList, // 提交状态
      parseTime: parseTime, // 时间处理
      timeValue: [],
      loading: false, // loading加载
      ticketList: [], // 列表数据
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  watch: {
    // 处理申报时间
    timeValue: {
      handler(newVal, oldVal) {
        if (newVal && newVal.length) {
          this.searchInfo.startOpeningTime = newVal[0]
          this.searchInfo.endOpeningTime = newVal[1]
        } else {
          this.searchInfo.startOpeningTime = ''
          this.searchInfo.endOpeningTime = ''
        }
      },
      deep: true
    }
  },
  created() {
    this.searchInfoCopy = JSON.parse(JSON.stringify(this.searchInfo))
    this.getList()
  },
  methods: {
    // 搜索
    onSearch() {
      this.pageInfo.currentPage = 1
      this.getList()
    },
    // 重置
    resetQuery() {
      this.timeValue = []
      this.searchInfo = JSON.parse(JSON.stringify(this.searchInfoCopy))
      this.getList()
    },
    // 分页
    onPagination(obj) {
      this.pageInfo.currentPage = obj.page
      this.pageInfo.pageSize = obj.limit
      this.getList()
    },
    // 列表数据
    getList() {
      const params = {
        ...this.searchInfo,
        ...this.pageInfo
      }
      doMyAuditApplication(params).then(res => {
        const data = res.records
        data.forEach(item => {
          const statusObj = this.circulationStatus && this.circulationStatus.find(it => it.value === item.status)
          const projectObj = this.projectClassList.find(it => it.value === item.projectClass)
          const auditStatusObj = this.auditStatusList.find(it => it.value === item.auditStatus)
          item.projectClassName = projectObj ? projectObj.label : ''
          item.statusName = statusObj ? statusObj.label : ''
          item.auditStatusName = auditStatusObj ? auditStatusObj.label : ''
        })
        this.ticketList = data
        this.pageInfo.total = res.total
      })
    },
    handleApply(row) {
      this.$refs.applyDialog.open(row)
    },
    // 表单数据弹窗
    handleView(row,btnShow) {
      // this.$refs.reviewDialog.open(row.process, row.id)
      this.$refs.reviewDialog.open(row,btnShow)
    },
    // 查看进度和审批历史弹窗
    handleHistoryView(row) {
      this.$refs.historyDialog.open(row, 2)
    }
  }
}
</script>

<style scoped></style>
